package com.cloudcross.ssp.web.back.main.role;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.model.Resources;
import com.cloudcross.ssp.model.Roles;
import com.cloudcross.ssp.service.IResourcesService;
import com.cloudcross.ssp.service.IRolesService;
import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.consts.Status;
import com.cloudcross.ssp.common.utils.LangUtil;
import com.cloudcross.ssp.common.utils.Pager;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * @author wuqiang.du 2013-11-19
 * @Email: mmm333zzz520@163.com
 * @version 1.0v
 */
@Controller
@RequestMapping("/back/main/role/")
public class RoleController extends SimpleController {
	@Autowired
	private IRolesService roleService;
	
	@Autowired
	private IResourcesService resourcesService;

//	@RequestMapping("list")
//	public String list(Model model, Resources menu, String pageNow) {
//		return "/back/main/role/list";
//	}

	/**
	 * 跑到新增界面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("add")
	public String addUI() {
		return page("create");
	}

	/**
	 * 修改角色信息
	 * 
	 * @param model
	 * @param r
	 * @param request
	 */
	@RequestMapping("edit")
	public String editUI(Model model, String roleId) {
		Roles role = roleService.getById(roleId);
		model.addAttribute("role", role);
		return page("edit");
	}

	/**
	 * 验证账号是否存在
	 * 
	 * @author wuqiang.du Email：mmm333zzz520@163.com date：2014-2-19
	 * @param name
	 * @return
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(String name) {
		Roles role = roleService.isExist(name);
        return role == null;
	}

	/**
	 * 删除
	 * 
	 * @param model
	 * @param videoTypeId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("deleteById")
	public Map<String, Object> deleteById(Model model, String ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		return map;
	}

	/**
	 * 获取角色信息列表
	 * 
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("list")
	public String index(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		int totalRow = 0;
		totalRow = roleService.countRolesByParams(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<Roles> rolesList = roleService.rolesList(paramMap, pager);
		model.addAttribute("pager", pager);
		model.addAttribute("rolesList", rolesList);
		return page("list");
	}

	/**
	 * 添加角色信息
	 * 
	 * @param model
	 * @param r
	 * @param request
	 */
	@RequestMapping("/addRole")
	public String save(Model model, @ModelAttribute("form") Roles r,
			HttpServletRequest request) {
		try {
			roleService.add(r);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return redirect("/back/main/role/list");
	}

	/**
	 * 修改角色信息
	 * 
	 * @param model
	 * @param r
	 * @param request
	 */
	
	@RequestMapping("/update")
	public String updateRoles(Model model, @ModelAttribute("form") Roles r,
			HttpServletRequest request) {
		Roles roles = new Roles();
		roles.setId(r.getId());
		roles.setDescription(r.getDescription());
		roles.setEnable(r.getEnable());
		roles.setName(r.getName());
		roles.setRoleKey(r.getRoleKey());
		roleService.updateRoles(roles);

		return redirect("/back/main/role/list");
	}

	/**
	 * 删除角色信息
	 * 
	 * 
	 * @param model
	 * @param r
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("deleteRole")
	public void deleteRoles(Integer id) {
		roleService.deleteRoles(id);
	}

	/**
	 * 单个，批量修改角色的enable为停用
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("unEnable")
	@ResponseBody
	public String enable(Long[] id) {
		if (ArrayUtils.isNotEmpty(id)) {
			roleService.updateStatus(LangUtil.array2List(id),
					Status.enable.value);
		}
		return OK;
	}

	/**
	 * 单个，批量修改角色的enable为开启
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("enable")
	@ResponseBody
	public String unEnable(Long[] id) {
		if (ArrayUtils.isNotEmpty(id)) {
			roleService.updateStatus(LangUtil.array2List(id),
					Status.disable.value);
		}
		return OK;
	}
	
	@RequestMapping("/permission")
	public String getMenuRole(Model model, Long roleId) {
		List<Resources> resourcesList = resourcesService.findAllRootResources();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		for (Resources res : resourcesList) {
			paramMap.put("id", res.getId());
			paramMap.put("type", 1);
			List<Resources> menuList = resourcesService.findAllResourceById(paramMap);
			for (Resources menu : menuList) {
				paramMap.put("id", menu.getId());
				paramMap.put("type", 2);
				List<Resources> buttonList = resourcesService.findAllResourceById(paramMap);
				menu.setChildren(buttonList);
			}
			res.setChildren(menuList);
		}
		String resStr = "";
		List<Map<String, Object>> resRolesList = roleService.getResourceByRoleId(roleId);
		for (Map<String, Object> map : resRolesList) {
			resStr += map.get("resc_id")+",";
		}
		if(resStr !=""){
			resStr = resStr.substring(0, resStr.length()-1);
		}
		model.addAttribute("resourcesList", resourcesList);
		model.addAttribute("resRoles", resStr);
		model.addAttribute("roleId", roleId);
		return page("permission");
	}
	
	@RequestMapping("/savePermission")
	public String saveMenuRole(Model model, Long roleId, String resRoles) {
		boolean flag = roleService.deleteResByRoleId(roleId);
		if(flag){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			String[] resArr = resRoles.split(",");
			paramMap.put("roleId", roleId);
			paramMap.put("resRoles", resArr);
			roleService.addResourcesBatch(paramMap);
		}
		return redirect("/back/main/role/list");
	}
}
